import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageNumber;
import com.spire.pdf.graphics.PdfGraphicsUnit;
import com.spire.pdf.graphics.PdfUnitConvertor;
import com.spire.pdf.widget.PdfPageCollection;
import com.wmd.core.operation.FileOperation;
import org.junit.jupiter.api.Test;

import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static com.wmd.core.operation.FileOperation.FormatUrlString;

/**
 * @projectName:DocumentLibrary
 * @packageName:PACKAGE_NAME
 * @Description:TODO
 * @Author:luyincheng
 * @CreateDate: 2022/1/2110:06
 */
public class ReadPdf {
   /**
    * @description
    * @author  luyincheng
    * @date 2022-01-21 10:40
    * @param
    * @return void
   */
   @Test
    public void test1() throws IOException {
        String urlString="http://192.168.30.245:8032///App_Files/ProjectFiles/2018/HZU18001F/2cf0a586-16c7-412d-a983-6c4f906e7b23.pdf";
       FileOperation.UrlFileInfo urlFileInfo=FileOperation.getUrlFileInfo(urlString);
       if(".pdf".equals(urlFileInfo.ext.toLowerCase())){
           URL url = new URL(FileOperation.FormatUrlString(urlString));
           HttpURLConnection httpConnectionAlerady = (HttpURLConnection) url.openConnection();

           PdfDocument document=new PdfDocument();
           document.loadFromStream(httpConnectionAlerady.getInputStream());
           PdfPageCollection pages = document.getPages();
           int pn= pages.getCount();
           List<int[]> pageInfo=new ArrayList<>();
           for(int i=0;i<pn;i++){
               PdfPageBase page = pages.get(i);
               //获取第一页页面宽度、高度
               float pointWidth = (float) page.getSize().getWidth();
               float pointHeight = (float) page.getSize().getHeight();

               //将度量单位转换为厘米
               PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
               float millimeterWidth = unitCvtr.convertUnits(pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Millimeter);
               float millimeterHeight = unitCvtr.convertUnits(pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Millimeter);

               int mmWidth=Math.round(millimeterWidth);
               int mmHeight=Math.round(millimeterHeight);
               pageInfo.add(new int[]{mmWidth,mmHeight});
               //输出页面大小
               System.out.println("该PDF的页面大小为(宽: " + pointWidth + "pt, 高: " + pointHeight + "pt).");
               System.out.println("该PDF的页面大小为(宽: " + millimeterWidth + "mm, 高: " + millimeterHeight + "mm.)");
               System.out.println("该PDF的页面大小为(宽: " + mmWidth + "mm, 高: " + mmHeight + "mm.)");

           }
       }
    }
}
